package com.cskaoyan.service;

import com.cskaoyan.bean.entity.Systems;
import com.cskaoyan.bean.entity.SystemExample;
import com.cskaoyan.bean.vo.SystemExpressVo;
import com.cskaoyan.bean.vo.SystemMallVo;
import com.cskaoyan.bean.vo.SystemOrderVo;
import com.cskaoyan.bean.vo.SystemWxVo;
import com.cskaoyan.mapper.SystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project2_market
 * @time: 2021/10/19
 * @author: yangshuai
 **/

@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    SystemMapper systemMapper;

    static SystemExample example;
    static SystemExample.Criteria criteria;

    /**
     * 获取商场配置
     * @return
     */
    @Override
    public SystemMallVo selectMall() {

        // 设置查询条件
        example = new SystemExample();
        criteria = example.createCriteria();
        criteria.andKeyNameLike("%mall%");

        List<Systems> systemsList = systemMapper.selectByExample(example);

        // 匹配前端参数
        SystemMallVo systemMallVo = new SystemMallVo();
        for (Systems system : systemsList) {
            if ("market_mall_name".equals(system.getKeyName())){
                systemMallVo.setMarket_mall_name(system.getKeyValue());
            }else if ("market_mall_address".equals(system.getKeyName())){
                systemMallVo.setMarket_mall_address(system.getKeyValue());
            }else if ("market_mall_longitude".equals(system.getKeyName())){
                systemMallVo.setMarket_mall_latitude(system.getKeyValue());
            }else if ("market_mall_latitude".equals(system.getKeyName())){
                systemMallVo.setMarket_mall_longitude(system.getKeyValue());
            }else if ("market_mall_qq".equals(system.getKeyName())){
                systemMallVo.setMarket_mall_qq(system.getKeyValue());
            }else if ("market_mall_phone".equals(system.getKeyName())){
                systemMallVo.setMarket_mall_phone(system.getKeyValue());
            }
        }
        return systemMallVo;
    }

    /**
     * 修改商场配置
     * @param systemMallVo
     */
    @Override
    public void updateMall(SystemMallVo systemMallVo) {

        Systems systems;

        /**
         * 修改market_mall_name
         */
        if (!"".equals(systemMallVo.getMarket_mall_name())){
            //设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_mall_name");

            systems = new Systems(systemMallVo.getMarket_mall_name());
            // 执行sql
            int affected = systemMapper.updateByExampleSelective(systems, example);
            System.out.println(affected);
        }

        /**
         * 修改market_mall_address
         */
        if (!"".equals(systemMallVo.getMarket_mall_address())){
            //设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_mall_address");

            systems = new Systems(systemMallVo.getMarket_mall_address());
            int affected1 = systemMapper.updateByExampleSelective(systems, example);
            System.out.println(affected1);

        }

        /**
         * 修改market_mall_longitude
         */
        if (!"".equals(systemMallVo.getMarket_mall_latitude())){
            //设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_mall_longitude");

            systems = new Systems(systemMallVo.getMarket_mall_latitude());
            int affected = systemMapper.updateByExampleSelective(systems, example);
            System.out.println(affected);

        }

        /**
         * 修改market_mall_latitude
         */
        if (!"".equals(systemMallVo.getMarket_mall_longitude())){
            //设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_mall_latitude");

            systems = new Systems(systemMallVo.getMarket_mall_longitude());
            int affected = systemMapper.updateByExampleSelective(systems, example);
            System.out.println(affected);

        }

        /**
         * 修改market_mall_qq
         */
        if (!"".equals(systemMallVo.getMarket_mall_qq())){
            //设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_mall_qq");

            systems = new Systems(systemMallVo.getMarket_mall_qq());
            int affected = systemMapper.updateByExampleSelective(systems, example);
            System.out.println(affected);

        }

        /**
         * 修改market_mall_phone
         */
        if (!"".equals(systemMallVo.getMarket_mall_phone())){
            //设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_mall_phone");

            systems = new Systems(systemMallVo.getMarket_mall_phone());
            int affected = systemMapper.updateByExampleSelective(systems, example);
            System.out.println(affected);

        }

    }

    /**
     * 查询运费配置
     * @return
     */
    @Override
    public SystemExpressVo selectExpress() {

        // 设置查询条件
        example = new SystemExample();
        criteria = example.createCriteria();
        criteria.andKeyNameLike("%express%");

        // 执行sql
        List<Systems> systemsList = systemMapper.selectByExample(example);

        // 添加到SystemExpressVo
        SystemExpressVo systemExpressVo = new SystemExpressVo();
        for (Systems system : systemsList) {
            if ("market_express_freight_min".equals(system.getKeyName())){
                systemExpressVo.setMarket_express_freight_min(system.getKeyValue());
            }else if ("market_express_freight_value".equals(system.getKeyName())){
                systemExpressVo.setMarket_express_freight_value(system.getKeyValue());
            }
        }
        return systemExpressVo;
    }

    /**
     * 更新运费配置
     * @param systemExpressVo
     */
    @Override
    public void updateExpress(SystemExpressVo systemExpressVo) {

        if (!"".equals(systemExpressVo.getMarket_express_freight_min()) || systemExpressVo.getMarket_express_freight_min() != null){
            // 设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_express_freight_min");

            Systems system1 = new Systems(systemExpressVo.getMarket_express_freight_min());

            systemMapper.updateByExampleSelective(system1,example);
        }


        if (!"".equals(systemExpressVo.getMarket_express_freight_min()) || systemExpressVo.getMarket_express_freight_min() != null){
            // 设置修改条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_express_freight_value");

            Systems system2 = new Systems(systemExpressVo.getMarket_express_freight_value());

            systemMapper.updateByExampleSelective(system2,example);
        }

    }

    /**
     * 查询订单配置
     * @return
     */
    @Override
    public SystemOrderVo selectOrder() {

        // 设置查询条件
        example = new SystemExample();
        criteria = example.createCriteria();
        criteria.andKeyNameLike("%order%");

        // 执行sql
        List<Systems> systemsList = systemMapper.selectByExample(example);

        // 匹配参数
        SystemOrderVo systemOrderVo = new SystemOrderVo();
        for (Systems system : systemsList) {
            if ("market_order_comment".equals(system.getKeyName())){
                systemOrderVo.setMarket_order_comment(system.getKeyValue());
            }else if ("market_order_unconfirm".equals(system.getKeyName())){
                systemOrderVo.setMarket_order_unconfirm(system.getKeyValue());
            }else if ("market_order_unpaid".equals(system.getKeyName())){
                systemOrderVo.setMarket_order_unpaid(system.getKeyValue());
            }
        }
        return systemOrderVo;
    }

    /**
     * 更新订单配置
     * @param systemOrderVo
     */
    @Override
    public void updateOrder(SystemOrderVo systemOrderVo) {


        // 修改market_order_comment
        if (!"".equals(systemOrderVo.getMarket_order_comment()) || systemOrderVo.getMarket_order_comment() != null){

            // 设置查询条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_order_comment");

            Systems systems = new Systems(systemOrderVo.getMarket_order_comment());
            systemMapper.updateByExampleSelective(systems,example);
        }

        // 修改market_order_unconfirm
        if (!"".equals(systemOrderVo.getMarket_order_unconfirm()) || systemOrderVo.getMarket_order_unconfirm() != null){

            // 设置查询条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_order_unconfirm");

            Systems systems = new Systems(systemOrderVo.getMarket_order_unconfirm());
            systemMapper.updateByExampleSelective(systems,example);
        }

        // 修改market_order_unpaid
        if (!"".equals(systemOrderVo.getMarket_order_unpaid()) || systemOrderVo.getMarket_order_unpaid() != null){

            // 设置查询条件
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_order_unpaid");

            Systems systems = new Systems(systemOrderVo.getMarket_order_unpaid());
            systemMapper.updateByExampleSelective(systems,example);
        }


    }

    /**
     * 查询小程序配置
     * @return
     */
    @Override
    public SystemWxVo selectWx() {

        // 设置查询条件
        example = new SystemExample();
        criteria = example.createCriteria();
        criteria.andKeyNameLike("%wx%");

        List<Systems> systemsList = systemMapper.selectByExample(example);

        // 匹配前端参数
        SystemWxVo systemWxVo = new SystemWxVo();
        for (Systems systems : systemsList) {

            if ("market_wx_catlog_goods".equals(systems.getKeyName())){
                systemWxVo.setMarket_wx_catlog_goods(systems.getKeyValue());
            }else if ("market_wx_catlog_list".equals(systems.getKeyName())){
                systemWxVo.setMarket_wx_catlog_list(systems.getKeyValue());
            }else if ("market_wx_index_brand".equals(systems.getKeyName())){
                systemWxVo.setMarket_wx_index_brand(systems.getKeyValue());
            }else if ("market_wx_index_hot".equals(systems.getKeyName())){
                systemWxVo.setMarket_wx_index_hot(systems.getKeyValue());
            }else if ("market_wx_index_new".equals(systems.getKeyName())){
                systemWxVo.setMarket_wx_index_new(systems.getKeyValue());
            }else if ("market_wx_index_topic".equals(systems.getKeyName())){
                systemWxVo.setMarket_wx_index_topic(systems.getKeyValue());
            }else if ("market_wx_share".equals(systems.getKeyName())){
                systemWxVo.setMarket_wx_share(systems.getKeyValue());
            }
        }
        return systemWxVo;
    }

    /**
     * 修改小程序配置
     * @param systemWxVo
     */
    @Override
    public void updateWx(SystemWxVo systemWxVo) {

        if (!"".equals(systemWxVo.getMarket_wx_catlog_goods()) || systemWxVo.getMarket_wx_catlog_goods() != null){
            // 获取sql条件语句
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_wx_catlog_goods");
            // 执行sql
            Systems systems = new Systems(systemWxVo.getMarket_wx_catlog_goods());
            systemMapper.updateByExampleSelective(systems,example);
        }

        if (!"".equals(systemWxVo.getMarket_wx_catlog_list()) || systemWxVo.getMarket_wx_catlog_list() != null){
            // 获取sql条件语句
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_wx_catlog_list");
            // 执行sql
            Systems systems = new Systems(systemWxVo.getMarket_wx_catlog_list());
            systemMapper.updateByExampleSelective(systems,example);
        }
        if (!"".equals(systemWxVo.getMarket_wx_index_brand()) || systemWxVo.getMarket_wx_index_brand() != null){
            // 获取sql条件语句
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_wx_index_brand");
            // 执行sql
            Systems systems = new Systems(systemWxVo.getMarket_wx_index_brand());
            systemMapper.updateByExampleSelective(systems,example);
        }
        if (!"".equals(systemWxVo.getMarket_wx_index_hot()) || systemWxVo.getMarket_wx_index_hot() != null){
            // 获取sql条件语句
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_wx_index_hot");
            // 执行sql
            Systems systems = new Systems(systemWxVo.getMarket_wx_index_hot());
            systemMapper.updateByExampleSelective(systems,example);
        }
        if (!"".equals(systemWxVo.getMarket_wx_index_new()) || systemWxVo.getMarket_wx_index_new() != null){
            // 获取sql条件语句
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_wx_index_new");
            // 执行sql
            Systems systems = new Systems(systemWxVo.getMarket_wx_index_new());
            systemMapper.updateByExampleSelective(systems,example);
        }
        if (!"".equals(systemWxVo.getMarket_wx_index_topic()) || systemWxVo.getMarket_wx_index_topic() != null){
            // 获取sql条件语句
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_wx_index_topic");
            // 执行sql
            Systems systems = new Systems(systemWxVo.getMarket_wx_index_topic());
            systemMapper.updateByExampleSelective(systems,example);
        }
        if (!"".equals(systemWxVo.getMarket_wx_share()) || systemWxVo.getMarket_wx_share() != null){
            // 获取sql条件语句
            example = new SystemExample();
            criteria = example.createCriteria();
            criteria.andKeyNameEqualTo("market_wx_share");
            // 执行sql
            Systems systems = new Systems(systemWxVo.getMarket_wx_share());
            systemMapper.updateByExampleSelective(systems,example);
        }
    }


}
